/**
 * Project Name:kany-utils
 * File Name:SwingResourceManager.java
 * Package Name:me.kany.utils.swt.frame.common
 * Date:2016年5月3日上午11:24:54
 * Copyright (c) 2016, Jason.Wang All Rights Reserved.
 *
*/

package me.kany.utils.swt.frame;

import java.awt.Image;
import java.awt.Toolkit;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.ImageIcon;

/**
 * ClassName:SwingResourceManager <br/>
 * Function: Swing的资源管理器. <br/>
 * Date: 2016年5月3日 上午11:24:54 <br/>
 * 
 * @author Jason.Wang
 * @version
 * @since JDK 1.7
 * @see
 */
public class SwingResourceManager {
	// 创建一个HashMap存放当前的图片对象，将图片对象用键值对的方式存储
	private static HashMap<String, Image> m_ClassImageMap = new HashMap<String, Image>();

	/**
	 * 
	 * getImage:通过文件流获取图片的信息. <br/>
	 *
	 * @author Jason.Wang
	 * @param inputStream
	 * @return
	 * @since JDK 1.7
	 */
	private static Image getImage(InputStream inputStream) {
		ByteArrayOutputStream baos = null;
		try {
			baos = new ByteArrayOutputStream();
			byte[] buf = new byte[4096];
			while (true) {
				int n = inputStream.read(buf);
				if (n == -1)
					break;
				baos.write(buf, 0, n);
			}
			return Toolkit.getDefaultToolkit().createImage(baos.toByteArray());
		} catch (Exception e) {
			if (null != baos) {
				try {
					baos.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
		return null;
	}

	/**
	 * 
	 * getImage:通过文件流的方式获取jar文件中的图片信息. <br/>
	 *
	 * @author Jason.Wang
	 * @param clazz
	 * @param path
	 * @return
	 * @since JDK 1.7
	 */
	public static Image getImage(Class<?> clazz, String path) {
		String key = clazz.getName() + '|' + path;
		Image image = (Image) m_ClassImageMap.get(key);
		if (image == null) {
			if ((path.length() > 0) && (path.charAt(0) == '/')) {
				String newPath = path.substring(1, path.length());
				image = getImage(new BufferedInputStream(clazz.getClassLoader().getResourceAsStream(newPath)));
			} else {
				image = getImage(clazz.getResourceAsStream(path));
			}
			m_ClassImageMap.put(key, image);
		}
		return image;
	}

	/**
	 * 
	 * getImage:根据给定的图片路径获取图片. <br/>
	 *
	 * @author Jason.Wang
	 * @param path
	 * @return
	 * @since JDK 1.7
	 */
	public static Image getImage(String path) {
		return getImage("default", path);
	}

	/**
	 * 
	 * getImage:通过配置的文件的节点信息获取图片. <br/>
	 *
	 * @author Jason.Wang
	 * @param section
	 * @param path
	 * @return
	 * @since JDK 1.7
	 */
	public static Image getImage(String section, String path) {
		String key = section + '|' + SwingResourceManager.class.getName() + '|' + path;
		Image image = (Image) m_ClassImageMap.get(key);
		if (image == null)
			try {
				FileInputStream fis = new FileInputStream(path);
				image = getImage(fis);
				m_ClassImageMap.put(key, image);
				fis.close();
			} catch (IOException e) {
				return null;
			}

		return image;
	}

	/**
	 * 
	 * clearImages:清除当前的图片存放的信息. <br/>
	 *
	 * @author Jason.Wang
	 * @param section
	 * @since JDK 1.7
	 */
	public static void clearImages(String section) {
		for (Iterator<String> iterator = m_ClassImageMap.keySet().iterator(); iterator.hasNext();) {
			// 获取当前的节点信息
			String key = iterator.next();
			// 如果当前的Key
			if (key.startsWith(section + "|")) {
				Image image = m_ClassImageMap.get(key);
				image.flush();
				iterator.remove();
			}
		}
	}

	/**
	 * 
	 * getIcon:通过当前的类获取图标文件. <br/>
	 *
	 * @author Jason.Wang
	 * @param clazz
	 * @param path
	 * @return
	 * @since JDK 1.7
	 */
	public static ImageIcon getIcon(Class<?> clazz, String path) {
		return getIcon(getImage(clazz, path));
	}

	/**
	 * 
	 * getIcon:获取默认的图片信息. <br/>
	 *
	 * @author Jason.Wang
	 * @param path
	 * @return
	 * @since JDK 1.7
	 */
	public static ImageIcon getIcon(String path) {
		return getIcon("default", path);
	}

	/**
	 * 
	 * getIcon:根据配置文件中的位置获取图片. <br/>
	 *
	 * @author Jason.Wang
	 * @param section
	 * @param path
	 * @return
	 * @since JDK 1.7
	 */
	public static ImageIcon getIcon(String section, String path) {
		return getIcon(getImage(section, path));
	}

	/**
	 * 
	 * getIcon:通过图片获取对应的图片Icon. <br/>
	 *
	 * @author Jason.Wang
	 * @param image
	 * @return
	 * @since JDK 1.7
	 */
	public static ImageIcon getIcon(Image image) {
		if (image == null)
			return null;
		return new ImageIcon(image);
	}
}
